﻿@{
    var gridExtension = Html.DevExpress().GridView(
        settings => {
            settings.Name = "gridView";
            settings.CallbackRouteValues = new { Action = "ContextMenuPartial", Controller = "Columns" };
            settings.SettingsBehavior.ConfirmDelete = true;
            settings.SettingsEditing.Mode = GridViewEditingMode.EditForm;
            settings.Settings.ShowGroupPanel = true;
            settings.Settings.ShowFooter = true;
            settings.SettingsContextMenu.Enabled = true;
            settings.SettingsBehavior.EnableCustomizationWindow = true;
            settings.SettingsEditing.AddNewRowRouteValues = new { Action = "AddNewRowContextMenuPartial", Controller = "Columns" };
            settings.SettingsEditing.UpdateRowRouteValues = new { Action = "UpdateRowContextMenuPartial", Controller = "Columns" };
            settings.SettingsEditing.DeleteRowRouteValues = new { Action = "DeleteRowContextMenuPartial", Controller = "Columns" };
            settings.Width = Unit.Percentage(100);
        
            settings.KeyFieldName = "ProductID";
            settings.Columns.Add("ProductName");
            settings.Columns.Add(c => {
                c.FieldName = "CategoryID";
                c.Caption = "Category Name";
                c.GroupIndex = 0;
                c.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;

                c.ColumnType = MVCxGridViewColumnType.ComboBox;
                var comoboxProperties = (ComboBoxProperties)c.PropertiesEdit;
                comoboxProperties.ValueType = typeof(int);
                comoboxProperties.ValueField = "CategoryID";
                comoboxProperties.TextField = "CategoryName";
                comoboxProperties.DataSource = NorthwindDataProvider.GetCategories();
            });
            settings.Columns.Add("QuantityPerUnit");
            settings.Columns.Add(c => {
                c.FieldName = "UnitPrice";
                c.ColumnType = MVCxGridViewColumnType.SpinEdit;

                var spinEditProperties = (SpinEditProperties)c.PropertiesEdit;
                spinEditProperties.DisplayFormatString = "c";
                spinEditProperties.DisplayFormatInEditMode = true;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 60000;
            });
            settings.Columns.Add(c => {
                c.FieldName = "UnitsInStock";
                c.ColumnType = MVCxGridViewColumnType.SpinEdit;

                var spinEditProperties = (SpinEditProperties)c.PropertiesEdit;
                spinEditProperties.MinValue = 0;
                spinEditProperties.MaxValue = 10000;
            });
            settings.Columns.Add(c => {
                c.FieldName = "Discontinued";
                c.ColumnType = MVCxGridViewColumnType.CheckBox;

                var checkBoxProperties = (CheckBoxProperties)c.PropertiesEdit;
                checkBoxProperties.AllowGrayed = true;
                checkBoxProperties.AllowGrayedByClick = false;
            });
        
            settings.PreRender = (s, e) => {
                var grid = s as MVCxGridView;
                if(grid != null)
                    grid.ExpandRow(1);
            };
        });
    
    if(ViewData["EditError"] != null) {
        gridExtension.SetEditErrorText((string)ViewData["EditError"]);
    }
}
@gridExtension.Bind(Model).GetHtml()